/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fandi.controller;

import com.fandi.model.CustomerModel;
import com.fandi.model.CustomerTableModel;
import com.fandi.util.Message;
import com.fandi.view.CustomerFrame;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Fandy
 */
public class CustomerController {
    private CustomerModel model;
    private CustomerTableModel tableModel;
    
    private boolean isUpdate;
    private int index;
    private String code;

    public void setModel(CustomerModel model) {
        this.model = model;
    }

    public void setTableModel(CustomerTableModel tableModel) {
        this.tableModel = tableModel;
    }
    
    public void insert(CustomerFrame view) {
        String code     = view.getTxtCustCode().getText();
        String name     = view.getTxtCustName().getText();
        String address  = view.getTxtCustAddr().getText();
        String email    = view.getTxtCustEmail().getText();
        String phone    = view.getTxtCustPhone().getText();
        String desc     = view.getTxtCustDesc().getText();
        
        try {
            if (code.equals("")) {
                Message.showMessage("Code is required", "Error", Message.ERROR);
            } else if (name.equals("")) {
                Message.showMessage("Name is required", "Error", Message.ERROR);
            } else if (address.equals("")) {
                Message.showMessage("Address is required", "Error", Message.ERROR);
            } else if (phone.equals("")) {
                Message.showMessage("Phone is required", "Error", Message.ERROR);
            } else {
                model.setCode(code);
                model.setName(name);
                model.setAddress(address);
                model.setEmail(email);
                model.setPhone(phone);
                model.setDescription(desc);

                if(isUpdate) {
                    model.update(code);
                } else {
                    model.insert();
                }
                
                Message.showMessage("Successfully saved!", "Information", Message.INFO);
                
                renderTableData(view, false, null);
                model.reset();
                
                view.getBtnRemove().setEnabled(false);
                view.getTbCustData().clearSelection();
                
                setTotalRecords(view);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    public void remove(CustomerFrame view) {
        try {
            int index   = view.getTbCustData().getSelectedRow();
            String code = (String) view.getTbCustData().getValueAt(index, 0);
            
            if (model.delete(code)) {
                Message.showMessage("Successfully deleted!", "Success", Message.INFO);
                renderTableData(view, false, null);
            }
            
            reset(view);
            
            setTotalRecords(view);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    
    public void reset(CustomerFrame view) {
        model.reset();
        isUpdate = false;
        view.getBtnRemove().setEnabled(false);
        view.getTbCustData().clearSelection();
    }

    public void renderTableData(CustomerFrame view, boolean isSearch, String strName) {
        List<CustomerModel> list = null;
        try {
            if (!isSearch) {
                list = model.selectAll();
            } else {
                list = model.searchBy(strName);
            }
            
            tableModel.setList(list);
            view.getTbCustData().setModel(tableModel);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    
    public void selectItem(CustomerFrame view) {
        try {
            index   = view.getTbCustData().getSelectedRow();
            code    = (String) view.getTbCustData().getValueAt(index, 0);
            CustomerModel data = model.selectById(code);
            
            model.setId(data.getId());
            model.setCode(data.getCode());
            model.setName(data.getName());
            model.setAddress(data.getAddress());
            model.setEmail(data.getEmail());
            model.setPhone(data.getPhone());
            model.setDescription(data.getDescription());
            
            isUpdate = true;
            view.getBtnRemove().setEnabled(true);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void search(CustomerFrame view) {
        String code = view.getTxtSearch().getText();
        renderTableData(view, true, code);
    }
    
    public String getTotalItems() {
        try {
            String count = String.valueOf(model.count());
            
            return count;
        } catch (SQLException ex) {
            Logger.getLogger(ItemController.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    public void setTotalRecords(CustomerFrame view) {
        view.getLblTotalRecords().setText(getTotalItems());
    }
}
